package org.vaadin.golfscoretracker.data;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.vaadin.appfoundation.i18n.FieldTranslation;
import org.vaadin.appfoundation.persistence.data.AbstractPojo;

@Entity
public class RoundSet extends AbstractPojo {

    private static final long serialVersionUID = -5372235548130149609L;

    @FieldTranslation(tuid = "round set date")
    @Temporal(TemporalType.DATE)
    private Date date;

    @FieldTranslation(tuid = "round set course")
    @ManyToOne
    private Course course;

    @OneToMany(mappedBy = "roundSet", cascade = CascadeType.ALL)
    private List<Round> rounds = new ArrayList<Round>();

    public void setRounds(List<Round> rounds) {
        this.rounds = rounds;
    }

    public List<Round> getRounds() {
        return rounds;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Date getDate() {
        return date;
    }

    public void addRound(Round round) {
        if (round == null) {
            throw new IllegalArgumentException("Round may not be null");
        }
        round.setRoundSet(this);
        getRounds().add(round);
    }

    public void removeRound(Round round) {
        if (round == null) {
            throw new IllegalArgumentException("Round may not be null");
        }

        getRounds().remove(round);
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public Course getCourse() {
        return course;
    }

}
